package com.qf.controller;


import com.qf.constants.Status;
import com.qf.model.SysUser;
import com.qf.page.PageResult;
import com.qf.page.QueryPageBean;
import com.qf.page.Result;
import com.qf.service.SysUserService;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("user")
public class SysUserController {

    @Autowired
    private SysUserService sysUserService;

    @RequestMapping("findAll")
    public Result findAll(){




        Result result = new Result();
        List<SysUser> sysUsers = sysUserService.findAll();
        if (ObjectUtils.isNotEmpty(sysUsers)){
            result.setData(sysUsers);
            result.setFlag(Status.SUCCESS.isFlag());
            result.setMessage(Status.SUCCESS.getMessage());
            return result;
        }
        result.setMessage(Status.FAIL.getMessage());
        result.setFlag(Status.FAIL.isFlag());
        return result;
    }


    /**
     *添加一个user
     * @param user
     * @return result
     */
    @RequestMapping("/add")
    public Result add(@RequestBody SysUser user){
        Result result = new Result();
        int i = sysUserService.add(user);
        if (i>0){
            result.setFlag(Status.SUCCESS.isFlag());
            result.setMessage(Status.SUCCESS.getMessage());
            return result;
        }
            result.setMessage(Status.FAIL.getMessage());
            result.setFlag(Status.FAIL.isFlag());
            return result;
    }

    /**
     * user回显
     * @param id
     * @return result
     */
    @RequestMapping("findById")
    public Result findById(@RequestParam("id") int id){
        Result result = new Result();
        SysUser user = sysUserService.findById(id);
        if (ObjectUtils.isNotEmpty(user)){
            result.setData(user);
            result.setFlag(Status.SUCCESS.isFlag());
            result.setMessage(Status.SUCCESS.getMessage());
            return result;
        }
            result.setMessage(Status.FAIL.getMessage());
            result.setFlag(Status.FAIL.isFlag());
            return result;
    }

    /**
     * 修改user
     * @param user
     * @return result
     */
    @RequestMapping("edit")
    public Result edit(@RequestBody SysUser user){
        Result result = new Result();
        int i = sysUserService.edit(user);
        if (i>0){
            result.setFlag(Status.SUCCESS.isFlag());
            result.setMessage(Status.SUCCESS.getMessage());
            return result;
        }
        result.setMessage(Status.FAIL.getMessage());
        result.setFlag(Status.FAIL.isFlag());
        return result;
    }

    /**
     * 删除user
     * @param id
     * @return result
     */
    @RequestMapping("delete")
    public Result delete(@RequestParam("id") int id){
        Result result = new Result();
        int i = sysUserService.delete(id);
        if (i>0){
            result.setFlag(Status.SUCCESS.isFlag());
            result.setMessage(Status.SUCCESS.getMessage());
            return result;
        }
        result.setMessage(Status.FAIL.getMessage());
        result.setFlag(Status.FAIL.isFlag());
        return result;
    }

    @RequestMapping("all")
    public PageResult all(@RequestBody QueryPageBean queryPageBean){



        PageResult pageResult = sysUserService.findPage(queryPageBean);
        return pageResult;
    }
}
